package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author tangsy
 * @Description TODO
 * @date 2022/9/17 14:11
 * @Version 1.0
 */
@Controller
@RequestMapping("community")
@SuppressWarnings({"unchecked", "rawtypes"})
public class CommunityController extends BaseController {

    @Reference
    private CommunityService communityService;

    @Reference
    private DictService dictService;

    @PreAuthorize("hasAuthority('community.show')")
    @RequestMapping
    public String index(Model model, HttpServletRequest request){
        Map<String, Object> filters = getFilters(request);

        PageInfo<Community> page = communityService.findPage(filters);

        List<Dict> dictList = dictService.findListByDictCode("beijing");

        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        model.addAttribute("areaList",dictList);

        return "community/index";
    }

    @PreAuthorize("hasAuthority('community.create')")
    @RequestMapping("create")
    public String create(Model model){

        List<Dict> areaList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);

        return "community/create";

    }

    @PreAuthorize("hasAuthority('community.edit')")
    @RequestMapping("edit/{id}")
    public String edit(@PathVariable Long id, Model model){

        Community community = communityService.getById(id);
        List<Dict> dictList = dictService.findListByDictCode("beijing");

        model.addAttribute("community",community);
        model.addAttribute("areaList",dictList);

        return "community/edit";
    }

    @PreAuthorize("hasAuthority('community.edit')")
    @RequestMapping("update")
    public String update(Community community){

        communityService.update(community);

        return "common/successPage";

    }

    @PreAuthorize("hasAuthority('community.create')")
    @RequestMapping("save")
    public String save(Community community){

        communityService.insert(community);

        return "common/successPage";

    }

    @PreAuthorize("hasAuthority('community.delete')")
    @RequestMapping("delete/{id}")
    public String delete(@PathVariable Long id){

        communityService.delete(id);

        return "redirect:/community";

    }


}
